﻿<%@ Application Language="C#" %>

<script runat="server">

    public string strTime = null;
    public int updatingFlag = 0;
    System.Threading.Thread thread;

    void Application_Init(object sender, EventArgs e)
    {
        //Log.WritLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":Application Init!", "");
        //// Code that runs on application startup
        ////Time_Task.Instance().ExecuteTask += new System.Timers.ElapsedEventHandler(Global_ExecuteTask);
        ////Time_Task.Instance().Interval = 1000 * 60;//表示间隔30 秒
        ////Time_Task.Instance().Start();
        //thread = new System.Threading.Thread(new System.Threading.ThreadStart(ExecuteTask)); 
        //thread.Name = "AutoUpdateForOTS";
        //thread.Start();
    }

    void Application_Start(object sender, EventArgs e)
    {
        //Log.WritLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":Application Start!", "");
        //// //Code that runs on application startup
        ////Time_Task.Instance().ExecuteTask += new System.Timers.ElapsedEventHandler(Global_ExecuteTask);
        ////Time_Task.Instance().Interval = 1000 * 60;//表示间隔30 秒
        ////Time_Task.Instance().Start();
        //thread = new System.Threading.Thread(new System.Threading.ThreadStart(ExecuteTask));
        //thread.Name = "AutoUpdateForOTS";
        //thread.Start();
    }
    void Global_ExecuteTask(object sender, System.Timers.ElapsedEventArgs e)
    {
        //在这里编写需要定时执行的逻辑代码
        strTime = DateTime.Now.ToString("HH:mm:ss");
        if (strTime != null)
        {
            Log.WritLog("Get in,beging", strTime);
            string[] temp = strTime.Split(':');
            if (temp.Length == 3)
            {
                if (updatingFlag == 0 && Convert.ToInt16(temp[0]) == 0
                    && (Convert.ToInt16(temp[1]) == 0 || (Convert.ToInt16(temp[1]) == 1 && Convert.ToInt16(temp[2]) == 0)))
                {
                    updatingFlag = 1;
                    ContractInfoGetFromOtherDB comm = new ContractInfoGetFromOtherDB();
                    //comm.UpdateTradingPlant();
                    updatingFlag = 0;
                    Log.WritLog("Get out, end", System.DateTime.Now.ToString());
                }
            }
        }
    }
    void ExecuteTask()
    {
        while (true)
        {
            //在这里编写需要定时执行的逻辑代码
            strTime = DateTime.Now.ToString("HH:mm:ss");
            if (strTime != null)
            {
                Log.WritLog("Get in,beging", strTime);
                string[] temp = strTime.Split(':');
                if (temp.Length == 3)
                {
                    if (updatingFlag == 0 && Convert.ToInt16(temp[0]) == 0
                    && (Convert.ToInt16(temp[1]) == 0 || (Convert.ToInt16(temp[1]) == 1 && Convert.ToInt16(temp[2]) == 0)))
                    {
                        updatingFlag = 1;
                        ContractInfoGetFromOtherDB comm = new ContractInfoGetFromOtherDB();
                        //comm.UpdateTradingPlant();                        
                        updatingFlag = 0;
                        Log.WritLog("Get out, end", System.DateTime.Now.ToString());
                    }
                }
            }
            System.Threading.Thread.CurrentThread.Join(1000 * 60);//阻止1分钟
        }
    }
    void Application_End(object sender, EventArgs e)
    {
        //RestartApplication();
    }

    void Application_Error(object sender, EventArgs e)
    {
        // Code that runs when an unhandled error occurs
        // Response.Redirect("~/error.aspx");
    }

    void Session_Start(object sender, EventArgs e)
    {
        // Code that runs when a new session is started
        
    }

    void Session_End(object sender, EventArgs e)
    {
        // Code that runs when a session ends. 
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer 
        // or SQLServer, the event is not raised.
        //RestartApplication();
    }

    protected void RestartApplication()
    {
        //  Code that runs on application shutdown
        Log.WritLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":Application End!", "");

        //下面的代码是关键，可解决IIS应用程序池自动回收的问题
        //System.Threading.Thread.Sleep(1000);
        //这里设置你的web地址，可以随便指向你的任意一个aspx页面甚至不存在的页面，目的是要激发Application_Start
        //string url = "http://www.qumiao.com";
        string url = System.Web.Configuration.WebConfigurationManager.AppSettings["MainWeb"];
        System.Net.HttpWebRequest myHttpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
        System.Net.HttpWebResponse myHttpWebResponse = (System.Net.HttpWebResponse)myHttpWebRequest.GetResponse();
        System.IO.Stream receiveStream = myHttpWebResponse.GetResponseStream();//得到回写的字节流
        //try
        //{
        //    System.Net.WebClient wc = new System.Net.WebClient();
        //    wc.Credentials = System.Net.CredentialCache.DefaultCredentials;
        //    //Byte[] pageData = 
        //    wc.OpenRead(url);
        //    wc.DownloadData(url);
        //    //conc.webStr = Encoding.Default.GetString(pageData);
        //}
        //catch
        //{ }
        Log.WritLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":Application End!", "");
    }
</script>
